package com.ruoyi.eqmonitor.phase1.rabbit;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.ruoyi.eqmonitor.phase1.domain.OptDevicePhase1;
import com.ruoyi.eqmonitor.phase1.domain.OptDevicePhase1MonitorData;
import com.ruoyi.eqmonitor.phase1.service.IOptDevicePhase1MonitorDataService;
import com.ruoyi.eqmonitor.phase1.service.IOptDevicePhase1Service;


/**
 * @Author : breeze
 **/
@Component
@ConditionalOnProperty(value = "spring.rabbitmq.enable")
@RabbitListener(queues = "UNITALK")
public class FanoutReceiver {

	@SuppressWarnings("serial")
	public static Map<String,String> Phase1MonitorMap = new HashMap<String,String>(){{
		put("A10", "液位高度");
		put("CC0007", "液位日平均高度");

	}};

	@Autowired
	private IOptDevicePhase1Service phase1Service;
	@Autowired
	private IOptDevicePhase1MonitorDataService devicePhase1MonitorDataService;
	private Map<String,OptDevicePhase1> phase1Devices = new HashMap<String,OptDevicePhase1>();

	//综合液位
	private String monitor_code = "A10";
	private String device_start = "PTAD_";

	@SuppressWarnings("rawtypes")
	@RabbitHandler
	public void process(Map message) {
		getLiquid(message);
	}

	/**
	 * 获取液位计的实时液位值
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getLiquid(Map message) {
		String devid = message.get("devid").toString();
		if(devid.startsWith(device_start)) {
			List<Map<String,Object>> datas = (List<Map<String, Object>>) message.get("real");
			List<Map<String,Object>> liquids = new ArrayList<Map<String,Object>>();
			OptDevicePhase1 phase1Device = null;
			if(phase1Devices.containsKey(devid)) {
				phase1Device = phase1Devices.get(devid);
			}else {
				phase1Device = phase1Service.selectOptDevicePhase1ByDeviceCode(devid);
				phase1Devices.put(devid, phase1Device);
			}
			if(null == phase1Device) {
				return;
			}

			for (int i = 0; i < datas.size(); i++) {
				Map<String,Object> data = datas.get(i);
				String s = data.get("s").toString();
				if(s.equals(devid + "_" + monitor_code)) {
					liquids.add(data);
				}
			}

			List<OptDevicePhase1MonitorData> devicePhase1MonitorDatas = new ArrayList<OptDevicePhase1MonitorData>();
			for (int i = 0; i < liquids.size(); i++) {
				Map<String,Object> liquid = liquids.get(i);
				OptDevicePhase1MonitorData devicePhase1MonitorData = new OptDevicePhase1MonitorData();
				devicePhase1MonitorData.setDeviceCode(phase1Device.getDeviceCode());
				devicePhase1MonitorData.setMonitorCode(monitor_code);
				devicePhase1MonitorData.setMonitorValue(Double.parseDouble(liquid.get("v").toString()));
				devicePhase1MonitorData.setMonitorTime(new Date(Long.parseLong(liquid.get("t").toString())));
				devicePhase1MonitorDatas.add(devicePhase1MonitorData);
			}
			devicePhase1MonitorDataService.saveBatch(devicePhase1MonitorDatas);
//			System.out.println(JSONObject.toJSONString(devicePhase1MonitorDatas));
		}
	}

}
